package com.ai.controller.quartz;

import com.ai.entity.quartz.QuartzJob;
import com.ai.service.quartz.IQuartzJobService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: 定时任务在线管理
 * @Version:V1.0
 */
@RestController
@RequestMapping("/sys/quartzJob")
@Slf4j
@Api(tags = "定时任务接口")
public class QuartzJobController {
    @Resource
    private IQuartzJobService quartzJobService;

    /**
     * 分页列表查询
     *
     * @param quartzJob
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public IPage queryPageList(QuartzJob quartzJob, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {

        return null;

    }

    /**
     * 添加定时任务
     *
     * @param quartzJob
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestBody QuartzJob quartzJob) {
        quartzJobService.saveAndScheduleJob(quartzJob);
        return "创建定时任务成功";
    }

    /**
     * 更新定时任务
     *
     * @param quartzJob
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public String eidt(@RequestBody QuartzJob quartzJob) {
        try {
            quartzJobService.editAndScheduleJob(quartzJob);
        } catch (SchedulerException e) {
            log.error(e.getMessage(), e);
            return "更新定时任务失败!";
        }
        return "更新定时任务成功!";
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public String delete(@RequestParam(name = "id", required = true) String id) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        if (quartzJob == null) {
            return "未找到对应实体";
        }
        quartzJobService.deleteAndStopJob(quartzJob);
        return "删除成功!";

    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public String deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return "参数不识别！";
        }
        for (String id : Arrays.asList(ids.split(","))) {
            QuartzJob job = quartzJobService.getById(id);
            quartzJobService.deleteAndStopJob(job);
        }
        return "删除定时任务成功!";
    }

    /**
     * 暂停定时任务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/pause")
    @ApiOperation(value = "暂停定时任务")
    public String pauseJob(@RequestParam(name = "id") String id) {
        QuartzJob job = quartzJobService.getById(id);
        if (job == null) {
            return "定时任务不存在！";
        }
        quartzJobService.pause(job);
        return "暂停定时任务成功";
    }

    /**
     * 启动定时任务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/resume")
    @ApiOperation(value = "恢复定时任务")
    public String resumeJob(@RequestParam(name = "id") String id) {
        QuartzJob job = quartzJobService.getById(id);
        if (job == null) {
            return "定时任务不存在！";
        }
        quartzJobService.resumeJob(job);
        return "恢复定时任务成功";
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public QuartzJob queryById(@RequestParam(name = "id", required = true) String id) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        return quartzJob;
    }


    /**
     * 立即执行
     *
     * @param id
     * @return
     */
    @GetMapping("/execute")
    public String execute(@RequestParam(name = "id", required = true) String id) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        if (quartzJob == null) {
            return "未找到对应实体";
        }
        try {
            quartzJobService.execute(quartzJob);
        } catch (Exception e) {
            //e.printStackTrace();
            log.info("定时任务 立即执行失败>>" + e.getMessage());
            return "执行失败!";
        }
        return "执行成功!";
    }
}
